package com.alinesno.cloud.portal.desktop.web.ucenter;

import java.io.IOException;
import java.net.URISyntaxException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alinesno.cloud.base.boot.entity.ManagerSourceGenerateEntity;
import com.alinesno.cloud.base.boot.service.IManagerSourceGenerateService;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean;
import com.alinesno.cloud.common.facade.response.ResponseBean;
import com.alinesno.cloud.common.facade.response.ResponseGenerator;
import com.alinesno.cloud.common.web.base.advice.TranslateCode;
import com.alinesno.cloud.common.web.base.controller.FeignMethodController;
import com.alinesno.cloud.common.web.login.aop.DataFilter;

/**
 * 代码生成器
 * 
 * @author WeiXiaoJin
 * @since 2019年4月8日 下午10:29:02
 */
@Controller
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("portal/ucenter/source")
public class SourceGenerateController extends FeignMethodController<ManagerSourceGenerateEntity, IManagerSourceGenerateService> {

	private static final Logger log = LoggerFactory.getLogger(SourceGenerateController.class);

	@Autowired
	private IManagerSourceGenerateService managerSourceGenerateService ; 

	@TranslateCode(value = "[{hasStatus:has_status}]")
	@DataFilter
	@ResponseBody
	@RequestMapping("/datatables")
	public DatatablesPageBean datatables(HttpServletRequest request, Model model, DatatablesPageBean page) {
		log.debug("page = {}", ToStringBuilder.reflectionToString(page));
		return this.toPage(model, managerSourceGenerateService, page);
	}

	/**
	 * 下载文件 (分布式部署待优化)
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/code")
	public ResponseEntity<byte[]> download(HttpServletRequest request, String id) throws IOException {
		byte[] body = managerSourceGenerateService.generatorCode(id) ; 
		
		ManagerSourceGenerateEntity e = this.getFeign().getOne(id) ; 
		
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Disposition", "attchement;filename=" + e.getFeignServerPath() + ".zip" ) ;
		HttpStatus statusCode = HttpStatus.OK;
		ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
		return entity;
	}

	/**
	 * 上传代码到git上面
	 * @param request
	 * @param id
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("/git")
	public ResponseBean git(HttpServletRequest request, String id) throws IOException {
		String path = managerSourceGenerateService.git(id) ; 
		return ResponseGenerator.ok(path);
	}
	
	@ResponseBody
	@RequestMapping("/buildStatus")
	public ResponseBean buildStatus(HttpServletRequest request, String id) throws IOException {
		String s = managerSourceGenerateService.isBuildDockerSuccess(id) ; 
		return ResponseGenerator.ok(s);
	}

	/**
	 * 构建镜像及状态
	 * @throws IOException 
	 */
	@RequestMapping("/dockerOpen")
	public void dockerOpen(Model model , String id) throws IOException {
		
		ManagerSourceGenerateEntity bean = managerSourceGenerateService.findEntityById(id) ; 
		String status = managerSourceGenerateService.isBuildDockerSuccess(bean) ; 
		bean.setDockerBuildStatus(status); 
		
		model.addAttribute("bean", bean) ; 
	}


	/**
	 * 配置中心
	 * @param request
	 * @param id
	 * @return
	 * @throws IOException
	 * @throws URISyntaxException 
	 */
	@ResponseBody
	@RequestMapping("/jenkins")
	public ResponseBean jenkins(HttpServletRequest request, String id) throws IOException, URISyntaxException {
		String path = managerSourceGenerateService.jenkins(id) ; 
		return ResponseGenerator.ok(path);
	}

	/**
	 * 与docker镜像集成
	 * @param request
	 * @param id
	 * @return
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	@ResponseBody
	@RequestMapping("/docker")
	public ResponseBean docker(HttpServletRequest request, String id) throws IOException, URISyntaxException {
		
		Assert.hasLength(id , "主键不能为空.");

		// TODO 待集成企业用户,企业用户可以有自己的k8s空间，然后集成域名发布(目前只支持开发环境，不支持生产环境发布)
		boolean isPublic = false ; 
		
		String path = managerSourceGenerateService.buildDocker(id, isPublic) ; 
		return ResponseGenerator.ok(path);
	}
	
	/**
	 * 持续集成
	 * @param request
	 * @param id
	 * @return
	 * @throws IOException
	 * @throws URISyntaxException 
	 */
	@ResponseBody
	@RequestMapping("/ci")
	public ResponseBean ci(HttpServletRequest request, String id) throws IOException, URISyntaxException {
		String path = managerSourceGenerateService.git(id) ; 
		path = managerSourceGenerateService.jenkins(id) ; 
		return ResponseGenerator.ok(path);
	}

	@Override
	public IManagerSourceGenerateService getFeign() {
		return managerSourceGenerateService;
	}

	/**
	 * 进入设置界面 
	 */
	@RequestMapping("/settings")
	public void settings() {
		log.debug("开发平台配置.");
	}

}
